#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 将Unicode文本标准化
问题: 你正在处理Unicode字符串，需要确保所有字符串在底层有相同的表示。
提示 :
"""
import unicodedata

def recipe1():
    s1 = 'Spicy Jalape\u00f1o'
    s2 = 'Spicy Jalapen\u0303o'
    print(f"{s1 = }")
    print(f"{s2 = }")
    print(f"{s1 == s2 = }")
    print(f"{len(s1) = }")
    print(f"{len(s2) = }")

    # 将文本标准化
    t1 = unicodedata.normalize('NFC', s1) #  NFC表示字符应该是整体组成
    t2 = unicodedata.normalize('NFC', s2)
    print(f"{t1 = }")
    print(f"{t2 = }")
    print(f"{t1 == t2 = }")
    print(f"{ascii(t1) = }")

    t3 = unicodedata.normalize('NFD', s1) #  NFC表示字符应该是整体组成
    t4 = unicodedata.normalize('NFD', s2)
    print(f"{t3 = }")
    print(f"{t4 = }")
    print(f"{t3 == t4 = }")
    print(f"{ascii(t3) = }")

def recipe2():
    """Python同样支持扩展的标准化形式NFKC和NFKD"""
    s = '\ufb01'
    print(f"{s = }")

    print(f"{unicodedata.normalize('NFD', s) = }")
    # Notice how the combined letters are broken apart here
    print(f"{unicodedata.normalize('NFKD', s) = }")
    print(f"{unicodedata.normalize('NFKC', s) = }")

def recipe3():
    """在清理和过滤文本的时候字符的标准化"""
    s1 = 'Spicy Jalape\u00f1o'
    print(f"{s1 = }")
    t1 = unicodedata.normalize('NFD', s1)
    print(f"{t1 = }")
    print(f"{''.join(c for c in t1 if not unicodedata.combining(c)) = }")

def recipe4():
    """中文"""
    s = '\u4f60\u597d Jone'
    print(f"{s = }")

    print(f"{unicodedata.normalize('NFC', s) = }")
    print(f"{ascii(unicodedata.normalize('NFD', s)) = }")

    print(f"{unicodedata.normalize('NFKD', s) = }")
    print(f"{unicodedata.normalize('NFKC', s) = }")

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()
    print('recipe3'.center(20, '*'))
    recipe3()    
    print('recipe4'.center(20, '*'))
    recipe4()  

if __name__ == '__main__':
    main()    